package com.simtop.controller;

import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.Sport;
import com.simtop.pojo.User;
import com.simtop.service.U3DSportService;
import com.simtop.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/u3d/sport")
@CrossOrigin
public class U3DSportController {
    private static final Logger logger = LoggerFactory.getLogger(U3DSportController.class);

    @Autowired
    private U3DSportService u3DSportService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
//    @ResponseBody
    public ServerResponse<String> addSportData(@RequestBody Sport sport, HttpServletRequest request){
        logger.info("u3d正在往web数据库插入报告数据"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        System.out.println("获取到的实验记录参数为："+sport);
        String token = request.getHeader("Authorization");
        System.out.println(token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DSportService.add(sport);
    }

    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Sport> findById(Integer id, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        System.out.println("token："+token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DSportService.findById(id);
    }

    @RequestMapping(value = "/findResult",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo<Sport>> findResult(int pageNum, int pageSize, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        System.out.println("token："+token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if (u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        System.out.println("pageNum："+pageNum+"pageSize："+pageSize);
        return u3DSportService.findResult(pageNum,pageSize);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> deleteById(int id, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        System.out.println(token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        System.out.println("id："+id);
        return u3DSportService.deleteById(id);
    }

    @RequestMapping(value = "/findByName",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo<Sport>> findByName(String name, int pageNum, int pageSize, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        System.out.println(token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        System.out.println("name："+name+",pageNum："+pageNum+",pageSize："+pageSize);
        return u3DSportService.findByName(name,pageNum,pageSize);
    }

    /**
     * 用户总数
     * @param request
     * @return
     */
    @RequestMapping(value = "/userAccount",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Integer>  findUserAccount(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        System.out.println(token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DSportService.findUserAccount();
    }

    /**
     * 实验总次数
     * @param request
     * @return
     */
    @RequestMapping(value = "/expAcct",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Integer> expAcct(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        System.out.println(token);
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt, User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return u3DSportService.expAcct();
    }
}